package com.vma.inter.task;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.vma.inter.task.model.Data;
import com.vma.inter.task.model.RootClass;
import com.vma.lottery.model.LotteryRecord;
import com.vma.lottery.vo.LotteryRecordVo;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * Created by chenzui on 2017/3/30.
 */
public class RecordPullTask implements ITask {
    HttpClient httpClient = new DefaultHttpClient();
    Gson gson = new Gson();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void stop() {
        System.out.println(format.format(new Date())+"---------RecordPullTask stop()");
    }

    public void run() {
        try{
            LotteryRecord latest = LotteryRecord.dao.findFirst("select * from lottery_record order by opentime desc");
            RootClass rootClass = gson.fromJson(doPost(),RootClass.class);
            List<Data> datas = rootClass.getData();
            List<LotteryRecord> list = new ArrayList<LotteryRecord>();
            for (Data data:datas) {
                if(!latest.getOpentime().before(format.parse(data.getOpentime()))){
                    continue;
                }
                String openCode = data.getOpencode();
                String[] codeArr = openCode.split(",");
                String[] left = codeArr[5].split("\\+");
                LotteryRecord  record   =   new LotteryRecord().setCode1(Integer.valueOf(codeArr[0]))
                        .setCode2(Integer.valueOf(codeArr[1]))
                        .setCode3(Integer.valueOf(codeArr[2]))
                        .setCode4(Integer.valueOf(codeArr[3]))
                        .setCode5(Integer.valueOf(codeArr[4]))
                        .setCode6(Integer.valueOf(left[0]))
                        .setCode7(Integer.valueOf(left[1]))
                        .setExpect(Integer.valueOf(data.getExpect().substring(4,data.getExpect().length())))
                        .setYears(Integer.valueOf(data.getExpect().substring(0,4)))
                        .setOpencode(data.getOpencode())
                        .setOpentime(format.parse(data.getOpentime()))
                        .setOpentimestamp(String.valueOf(data.getOpentimestamp()));
                list.add(record);
            }
            if(list.size() != 0){
                Db.batchSave(list,list.size());
            }
            Thread.sleep(6000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private String doPost() throws IOException {
        String url="http://c.apiplus.net/newly.do?token=241d71ea7d144963&code=hk6&format=json";
        //POST的URL
        HttpPost httppost=new HttpPost(url);
        //建立HttpPost对象
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        //添加参数
        httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        //设置编码
        HttpResponse response=httpClient.execute(httppost);
        //发送Post,并返回一个HttpResponse对象
        if(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
            String result= EntityUtils.toString(response.getEntity());
            System.out.print("============"+result+"============");
            return result;
        }
        return "";
    }

    public static void main(String[] args){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date t1 = format.parse("2016-01-01 23:00:10");
            Date t2 = format.parse("2016-01-01 23:00:01");
            System.out.println(t1.before(t2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
